package com.poker.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poker.base.Result;
import com.poker.entity.LoginLog;
import com.poker.entity.User;
import com.poker.service.UserService;

import com.poker.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;


@Api(value = "用户模块",tags = "用户模块")
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取用户数据记录")
    @GetMapping("/detail")
    public Result<User> getUser(@RequestParam String userId){
        return Result.success(userService.getOneUser(userId));
    }

    @ApiOperation(value = "删除某一条用户登录记录")
    @GetMapping("/delLog")
    public Result<String> delLog(@RequestParam String id){
        return userService.delLog(id);
    }

    @ApiOperation(value = "登录接口")
    @GetMapping("/login")
    public Result<User> getUser(@RequestParam String userId,@RequestParam String password,HttpServletRequest request) throws UnknownHostException {
        return userService.login(userId,password, request);
    }


    @ApiOperation(value = "登出接口")
    @GetMapping("/logout")
    public Result<String> logout(){
        return userService.logout();

    }

    @ApiOperation(value = "查询登录记录")
    @GetMapping("/loginLog")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size",value = "每页条数",dataType = "string",paramType = "query",required = true,example = "10"),
            @ApiImplicitParam(name = "current",value = "当前页码",dataType = "string",paramType = "query",required = true,example = "1")
    })
    public Result<IPage<LoginLog>> getLoginLog(HttpServletRequest request){
        Page<LoginLog> pageReq = new Page<>();
        pageReq.setCurrent(Integer.valueOf(request.getParameter("current")));
        pageReq.setSize(Integer.valueOf(request.getParameter("size")));
        return Result.success(userService.getLoginLog(pageReq));
    }


    @ApiOperation(value = "转化为BASE64二进制流")
    @GetMapping("/base64")
    public Result<String> imageToBase64(@RequestParam String fullPath){
        return Result.success(FileUtils.imageToBase64(fullPath));
    }






}
